package data.objects;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBAttribute;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBHashKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBIgnore;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBRangeKey;
import com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBTable;

import data.dynamodbfunctions.Persistence_Helper_PubForum;
import data.dynamodbfunctions.Persistence_Helper_PubThread;
import data.exceptions.Dynamo_DB_Functions_Exception;
import data.meta.MetaAnnotations.MetaAssociation;
import data.meta.MetaAnnotations.MetaMethod;
import data.meta.MetaAnnotations.MetaParam;
import data.meta.Metadata;
/**
 * Ein Froum Objekt
 * @author Alexander
 *
 */
@DynamoDBTable(tableName="CC_Forum")
public class PubForum {
	
	public String TenantID;
	public String ID;
	public String ersteller;
	public String admin;
	public String erstelldatum;
	public String beschreibung;
	public String name;	
	
	public PubForum() {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SS");
		Date date = new Date();	
		this.ID = ""+UUID.randomUUID();
		this.erstelldatum = ""+dateFormat.format(date);
	}
	
	public PubForum(String TenantID, String admin, String beschreibung,
			String forumname, String ersteller) {
		super();
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SS");
		Date date = new Date();
		this.TenantID =TenantID;
		this.ID =""+UUID.randomUUID();
		this.ersteller = ersteller;
		this.admin = admin;
		this.erstelldatum = ""+dateFormat.format(date);
		this.beschreibung = beschreibung;
		this.name = forumname;
	}

	@DynamoDBHashKey(attributeName="ID")
	public String getTenantID() {return TenantID;}
	public void setTenantID(String thread_id) {this.TenantID = thread_id;}
	
	@DynamoDBRangeKey(attributeName="Range_ID")
	public String getForum_id(){ return  ID;}
	public void setForum_id(String forum_id) {this.ID = forum_id;}
	
	@DynamoDBAttribute(attributeName="Ersteller")
	public String getErsteller() {return ersteller;}
	public void setErsteller(String ersteller) {this.ersteller = ersteller;}
	
	@DynamoDBAttribute(attributeName="Admin")
	public String getAdmin() {return admin;}
	public void setAdmin(String admin) {this.admin = admin;}
	
	@DynamoDBAttribute(attributeName="Erstelldatum")
	public String getErstelldatum() {return erstelldatum;}
	public void setErstelldatum(String erstelldatum) {this.erstelldatum = erstelldatum;}
	
	@DynamoDBAttribute(attributeName="Beschreibung")
	public String getBeschreibung() {return beschreibung;}
	public void setBeschreibung(String beschreibung) {this.beschreibung = beschreibung;}
	
	@DynamoDBAttribute(attributeName="Name")
	public String getName() {return name;}
	public void setName(String forumname) {this.name = forumname;}
	
	@DynamoDBIgnore
	public static Metadata getMetaData(){
		Metadata meta= ClassSpy.getMetadata( "data.objects.PubForum" );
		return meta;
	}

	@MetaAssociation(source = "PubForum", target = "PubThread", name = "getThreads") 
	public static Set<String> getThreads(String TenantID, String id) throws Dynamo_DB_Functions_Exception{
		Set<String> threadIDs = new HashSet<String>();
		List<PubThread> readobjects = Persistence_Helper_PubThread.getInstance().readAllwithHashKey(id);
		for(PubThread a:readobjects){
			threadIDs.add(a.getThread_id());
		}	
		return threadIDs;
	}

//	@MetaMethod
	@DynamoDBIgnore
	public static PubForum read(@MetaParam("TenantID") String TenantID, @MetaParam("ID") String id) throws Dynamo_DB_Functions_Exception {
		PubForum readobject=Persistence_Helper_PubForum.getInstance().read(TenantID,id);
		return readobject;
	}
	
//	@MetaMethod
	@DynamoDBIgnore
	public static PubForum delete(@MetaParam("TenantID") String TenantID, @MetaParam("ID") String id) throws Dynamo_DB_Functions_Exception {
		return Persistence_Helper_PubForum.getInstance().delete(TenantID,id);
	}
	
//	@MetaMethod
	@DynamoDBIgnore
	public static PubForum update(@MetaParam("TenantID") String TenantID, @MetaParam("ID") String id, 
			@MetaParam("admin") String admin, @MetaParam("name") String name, 
			@MetaParam("beschreibung") String beschreibung) throws Dynamo_DB_Functions_Exception {
		PubForum updateForum =Persistence_Helper_PubForum.getInstance().read(TenantID,id);
		if(!"".equals(admin)){
			updateForum.setAdmin(admin) ;
		}
		if(!"".equals(beschreibung)){
			updateForum.setBeschreibung(beschreibung);
		}
		if(!"".equals(name)){
			updateForum.setName(name);
		}	
		return Persistence_Helper_PubForum.getInstance().update(updateForum);
	}
	
	@MetaMethod
	@DynamoDBIgnore
	public static PubForum create(@MetaParam("TenantID") String TenantID, @MetaParam("admin") String admin, 
			@MetaParam("name") String name, @MetaParam("beschreibung") String beschreibung,
			@MetaParam("ersteller") String ersteller) throws Dynamo_DB_Functions_Exception {
		PubForum createForum = new PubForum(TenantID, admin, beschreibung, name, ersteller);
		return Persistence_Helper_PubForum.getInstance().create(createForum);	
	}
	
}


	